/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p2a;

/**
 *
 * @author user
 */
import java.net.InetAddress;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Random;
import java.util.StringTokenizer;

public class Helper {

    public static String prepareIP(String IP) {
        StringBuffer s = new StringBuffer();

        CharacterIterator it = new StringCharacterIterator(IP);
        for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
            switch (ch) {
                case '*':
                    s.append(new Random().nextInt(254));
                    break;
                default:
                    s.append(ch);
                    break;
            }
        }
        return s.toString();
    }

    public static int preparePort(String P) {
        int port = 0;
        try {
            port = Integer.parseInt(P);
        } catch (Exception e) {
            port = new Random().nextInt(1024);
        }
        return port;
    }

    public static int getCharCount(String str, char c) {
        int count = str.replaceAll("[^" + c + "]", "").length();
        return count;
    }

    public static long ip2long(InetAddress ip) {
        long l = 0;
        byte[] addr = ip.getAddress();
        if (addr.length == 4) { //IPV4
            for (int i = 0; i < 4; ++i) {
                l += (((long) addr[i] & 0xFF) << 8 * (3 - i));
            }
        } else { //IPV6
            return 0;  // I dont know how to deal with these
        }
        return l;
    }

    public static long host2long(String host) {
        long ip = 0;
        if (!Character.isDigit(host.charAt(0))) {
            return -1;
        }
        int[] addr = ip2intarray(host);
        if (addr == null) {
            return -1;
        }
        for (int i = 0; i < addr.length; ++i) {
            ip += ((long) (addr[i] >= 0 ? addr[i] : 0)) << 8 * (3 - i);
        }
        return ip;
    }

    public static int[] ip2intarray(String host) {
        int[] address = {-1, -1, -1, -1};
        int i = 0;
        StringTokenizer tokens = new StringTokenizer(host, ".");
        if (tokens.countTokens() > 4) {
            return null;
        }
        while (tokens.hasMoreTokens()) {
            try {
                address[i++] = Integer.parseInt(tokens.nextToken()) & 0xFF;
            } catch (NumberFormatException nfe) {
                return null;
            }
        }
        return address;
    }

    public static String long2dotted(long address) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0, shift = 24; i < 4; i++, shift -= 8) {
            long value = (address >> shift) & 0xff;
            sb.append(value);
            if (i != 3) {
                sb.append('.');
            }
        }
        return sb.toString();
    }
}
